package com.hxc.orangeyule.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.hxc.orangeyule.R;
import com.hxc.orangeyule.callback.DataCallBack;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

/**
 * Created by Administrator on 2016/3/14.
 */
public abstract class MyBaseFragment extends Fragment {

    private HttpUtils mHttpUtils = new HttpUtils();
    private HttpHandler mHttpHandler;

    protected void getDataFragment(String path, final DataCallBack callBack) {
        mHttpHandler = mHttpUtils.send(HttpRequest.HttpMethod.GET, path, new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                if (!TextUtils.isEmpty(responseInfo.toString())) {
                    callBack.onSuccessed(responseInfo.toString());
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Toast.makeText(getActivity(), R.string.net_error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public  void onCreate(Bundle savedInstanceState){
        initData();
        setData();
        setListener();
    }

    protected abstract void setListener();

    protected abstract void setData();

    protected abstract void initData();

    @Override
    public abstract View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);
}
